{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Download Twitter Data for Sentiment Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use a dataset that contains 1.6 million training and 350 test tweets from 2009 with algorithmically assigned binary positive and negative sentiment scores that are fairly evenly split."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T00:31:58.308350Z",
     "start_time": "2020-06-21T00:31:57.790270Z"
    }
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import requests\n",
    "from io import BytesIO\n",
    "from zipfile import ZipFile"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download and unzip"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Download the data from [here](http://cs.stanford.edu/people/alecmgo/trainingandtestdata.zip)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following code downloads and extracts the content of the compressed file and stores it in 'sentiment140', while renaming the content as follows:\n",
    "- `training.1600000.processed.noemoticon.csv` to `train.csv`, and\n",
    "- `testdata.manual.2009.06.14.csv` to `test.csv`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T00:25:19.229380Z",
     "start_time": "2020-06-21T00:25:19.224873Z"
    }
   },
   "outputs": [],
   "source": [
    "path = Path('sentiment140')\n",
    "if not path.exists():\n",
    "    path.mkdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T00:25:19.236716Z",
     "start_time": "2020-06-21T00:25:19.230823Z"
    }
   },
   "outputs": [],
   "source": [
    "URL = 'http://cs.stanford.edu/people/alecmgo/trainingandtestdata.zip'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T00:26:09.959959Z",
     "start_time": "2020-06-21T00:25:26.510947Z"
    }
   },
   "outputs": [],
   "source": [
    "response = requests.get(URL).content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T00:28:27.896171Z",
     "start_time": "2020-06-21T00:28:23.826570Z"
    }
   },
   "outputs": [],
   "source": [
    "with ZipFile(BytesIO(response)) as zip_file:\n",
    "    for i, file in enumerate(zip_file.namelist()):\n",
    "        if file.startswith('train'):\n",
    "            local_file = path / 'train.csv'\n",
    "        elif file.startswith('test'):\n",
    "            local_file = path / 'test.csv'\n",
    "        else:\n",
    "            continue\n",
    "        with local_file.open('wb') as output:\n",
    "            for line in zip_file.open(file).readlines():\n",
    "                output.write(line)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:ml4t] *",
   "language": "python",
   "name": "conda-env-ml4t-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
